import Utils from './utils';

const BuryingPoint = {};
BuryingPoint.install = function(Vue, options) {
  Vue.directive('inputHandler', {
    bind(el, binding, vnode) {
      if (vnode.tag == 'input') {
        // 原生input
        Utils.inputAction(el, vnode, options);
        return;
      }
      // vux x-input组件
      let me = el.getElementsByTagName('input')[0];
      Utils.inputAction(me, vnode, options);
    },
    update(value) {},
    unbind(el) {}
  });

  Vue.prototype.$monitor = {
    onClick(vueComponent) {
      vueComponent._startTime = new Date().getTime();
      let elementName = '';
      try {
        elementName = window.event.target.innerText;
      } catch (error) {
        elementName = Utils.getMethodName();
      }
      elementName = Utils.filterChar(elementName);

      let event = {
        eventName: 'clickAction',
        startTime: new Date().getTime(),
        elementName,
        elementType: '按钮',
        pageIndex: Utils._pageIndex,
        pageName: Utils._pageName
      };
      Utils.sendEvent(event, vueComponent, options);
    },

    onCreate(vueComponent) {
      vueComponent._createPageTime = new Date().getTime();
      Utils._pageName = Utils.getName(vueComponent);
      Utils._pageIndex = Utils.getIndex(vueComponent);
    },

    onMounted(vueComponent) {
      vueComponent._entryDate = new Date().getTime();
      vueComponent._pageType = 'wap页';
    },

    // 导航离开时
    onBeforeRouteLeave(vueComponent) {
      vueComponent._endTime = new Date().getTime();
      let event = {
        eventName: 'reviewPage',
        pageName: Utils._pageName,
        pageIndex: Utils._pageIndex,
        pageType: vueComponent._pageType,
        startTime: vueComponent._createPageTime,
        entryDate: vueComponent._entryDate,
        endTime: vueComponent._endTime
      };
      if (Utils.judgePage(vueComponent)) {
        Utils.sendEvent(event, vueComponent, options);
      }
    },

    onDestroy(vueComponent) {
      let remainTime = new Date().getTime() - vueComponent._createPageTime;
    }
  };
};
export default BuryingPoint;
